package com.derbtec.refale.activities;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.derbtec.refale.R;
import com.derbtec.refale.models.Event;
import com.derbtec.refale.util.Session;

public class EventListViewAdapter extends BaseAdapter {
    private Context context;

    private List<Event> listEvent;

    public EventListViewAdapter(Context context, List<Event> listEvent) {
        this.context = context;
        this.listEvent = listEvent;
    }

    public int getCount() {
        return listEvent.size();
    }

    public Object getItem(int position) {
        return listEvent.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
        Event entry = listEvent.get(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.event_row, null);
        }
        
        //SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");
        TextView name = (TextView) convertView.findViewById(R.id.tvName);
        name.setText(entry.getName());
        
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yy kk:mm");

        TextView date = (TextView) convertView.findViewById(R.id.tvDate);
        date.setText((df.format(entry.getStartDate()).toString()) + " - " + (df.format(entry.getEndDate()).toString()));
        

        TextView place = (TextView) convertView.findViewById(R.id.tvPlace);
        place.setText(entry.getPlace());

        TextView creator = (TextView) convertView.findViewById(R.id.tvCreator);
        creator.setText(entry.getCreator().getUsername());
        
        if(Session.getUserid() == entry.getCreator().getId())
        	creator.setTextColor(Color.parseColor("#00BFFF"));
        else if(entry.getCreator().isAdmin())
        	creator.setTextColor(Color.parseColor("#FF0000"));
        else
        	creator.setTextColor(Color.parseColor("#FFFFFF"));

        //TextView commits = (TextView) convertView.findViewById(R.id.tvCommits);
        //commits.setText(((Integer)entry.getParticipants().size()).toString());

        return convertView;
    }

}